<template>
  <div>
    <a-table
      :columns="columns"
      :pagination="pagination"
      @change="handleTableChange"
      :data-source="copyDiscountList"
      :rowKey="
        (record, index) => {
          return index
        }
      "
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
      </span>
      <span slot="goods" slot-scope="text, record">
        <ListInfo :record="record.boxDetails"></ListInfo>
      </span>
      <!-- <span slot="price" slot-scope="text, record">
      <p class="red special-price" v-if="record.boxDetails.vipPrice">¥ {{ record.boxDetails.vipPrice }}</p>
      <p class="red special-price" v-else-if="record.boxDetails.productFavorablePrice">¥ {{ record.boxDetails.productFavorablePrice }}</p>
      <p :class="[record.boxDetails.vipPrice || record.boxDetails.productFavorablePrice ? 'has-special' : '', 'origin-price']">¥ {{ text }}</p>
    </span> -->
      <span slot="status" slot-scope="text, record">
        {{ record.boxReturnDetails.retStatus | goodsStatus }}
      </span>
      <span slot="action" slot-scope="text, record">
        <div v-if="record.boxReturnDetails.retStatus == 1">
          <a-popconfirm title="确定移除？" @confirm="removeClick(record)">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a href="javascript:;">移除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删款？" @confirm="handleMatchDelete(record)">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a>删款</a>
          </a-popconfirm>
          <!-- <a-divider type="vertical" />
          <a @click="enterClick(record)" v-if="!record.boxReturnDetails.falg">入库</a>
          <span v-else>
            <a-input placeholder="请填写oms实际入库商品编号" v-model="record.boxReturnDetails.omsNumber" style="width: 200px; margin-right: 8px" />
            <a @click="determine(record.boxReturnDetails)">确定</a>
          </span> -->
          <a-divider type="vertical" />
          <a @click="syncClick(record)">同步</a>
        </div>
        <div v-if="record.boxReturnDetails.retStatus == 2">
          <span v-if="record.boxReturnDetails.memo">实际入库:{{ record.boxReturnDetails.memo.substring(record.boxReturnDetails.memo.indexOf(':') + 1) }}</span>
          <span v-else>实际入库:{{ record.boxReturnDetails.memo }}</span>
        </div>
      </span>
    </a-table>
  </div>
</template>
<script>
import ListInfo from '@views/box/components/goodsInfo/listInfo'
import { forceReturnProduct, delBoxReturnDetailById, delBoxDetailAndBoxReturnDetail, syncWeChatReturnStatus } from '@/api/box/order'
const columns = [
  {
    title: '序号',
    align: 'center',
    scopedSlots: { customRender: 'serial' },
    width: '10%'
  },
  {
    title: '还货商品',
    dataIndex: 'barcode',
    scopedSlots: { customRender: 'goods' },
    width: '30%'
  },
  // {
  //   title: '商品单价(元)',
  //   dataIndex: 'boxDetails.productPrice',
  //   scopedSlots: { customRender: 'price' },
  //   width: '15%'
  // },
  {
    title: '状态',
    dataIndex: 'status',
    width: '15%',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    width: '40%'
  }
]
export default {
  props: {
    tableData: {
      type: Array,
      required: true
    },
    boxId: {
      default: null,
      required: true
    },
    boxReturnId: {
      default: null,
      required: true
    }
  },
  components: {
    ListInfo
  },
  filters: {
    goodsStatus(v) {
      if (v == 1) {
        return '待入库'
      } else if (v == 2) {
        return '已入库'
      }
    }
  },

  data() {
    return {
      columns,
      copyDiscountList: [],
      pagination: {
        pageSize: 10,
        page: 1,
        current: 1
      }
    }
  },
  watch: {
    tableData: {
      //深度监听，可监听到对象、数组的变化
      handler(newV, oldV) {
        this.copyDiscountList = newV
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination.page = pagination.page
      this.pagination.pageSize = pagination.pageSize
      this.pagination.current = pagination.current
    },
    // 删款
    handleMatchDelete(record) {
      let params = {
        requestData: {
          boxReturnId: this.boxReturnId,
          boxId: this.boxId,
          list: [
            {
              boxDetailId: record.boxReturnDetails.boxDetailId,
              boxReturnDetailId: record.boxReturnDetails.id
            }
          ]
        }
      }
      delBoxDetailAndBoxReturnDetail(params).then(res => {
        if (res.code == 0) {
          this.$message.success('删款成功!')
          this.$emit('refresh')
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    //移除
    removeClick(record) {
      let params = {
        requestData: {
          id: record.boxReturnDetails.id
        }
      }
      delBoxReturnDetailById(params).then(res => {
        if (res.code == 0) {
          this.$message.success('移除成功!')
          this.$emit('refresh')
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 入库
    enterClick(record) {
      let list = JSON.parse(JSON.stringify(this.copyDiscountList))
      list.forEach(ele => {
        if (ele.boxReturnDetails.id == record.boxReturnDetails.id) {
          ele.boxReturnDetails.falg = true
        }
      })
      this.copyDiscountList = list
    },
    determine(record) {
      if (!record.omsNumber) {
        this.$message.warning('请填写oms实际入库商品编号')
        return
      }
      let params = {
        requestData: {
          boxId: this.boxId,
          boxReturnDetailId: record.id,
          boxReturnId: record.boxReturnId,
          sku: record.omsNumber // oms商品编号
        }
      }
      forceReturnProduct(params).then(res => {
        if (res.code == 0) {
          this.$message.success('入库成功')
          this.$emit('refresh')
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 同步
    syncClick(record) {
      syncWeChatReturnStatus(record.boxReturnDetails.id).then(res => {
        if (res.code == 0) {
          this.$message.success('同步成功')
          this.$emit('refresh')
        } else {
          this.$message.warning(res.msg)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.red {
  color: #f45555;
  margin-right: 24px;
  font-size: 20px;
}
.special-price {
  font-size: 14px;
  margin-right: 0;
}
.has-special {
  text-decoration: line-through;
}
.origin-price,
.special-price {
  margin-bottom: 4px;
}
</style>

